/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.Utilities
*        文件名:             BaseInterfaceLogic.cs
*        创建系统时间:       2012/11/2 19:07:07
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.Utilities
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Reflection;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraTreeList.Nodes;
    using DevExpress.XtraEditors;

    public class BaseInterfaceLogic
    {
        public string FileName = "Log.txt";
        public static TreeListNode TargetNode;

        public static bool CheckInputModifyAnyOne(DataTable dataTable)
        {
            bool flag = false;
            flag = IsModfiedAnyOne(dataTable);
            if (!flag)
            {
               XtraMessageBox.Show(AppMessage.MSG0004, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            return flag;
        }

        public static bool CheckInputSelect(DataTable dataTable, string fieldSelected)
        {
            return CheckInputSelect(dataTable, fieldSelected, true);
        }

        public static bool CheckInputSelect(DataTable dataTable, string fieldSelected, bool needSelected)
        {
            bool flag = true;
            int num = 0;
            foreach (DataRow row in dataTable.Rows)
            {
                bool flag2 = true;
                if (row[fieldSelected].ToString() == flag2.ToString())
                {
                    num++;
                }
                if (num > 1)
                {
                    break;
                }
            }
            if (needSelected && (num == 0))
            {
               XtraMessageBox.Show(AppMessage.MSG0024, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                flag = false;
            }
            if (num > 1)
            {
               XtraMessageBox.Show(AppMessage.MSG0023, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                flag = false;
            }
            return flag;
        }

        public static bool CheckInputSelectAnyOne(DataTable dataTable, string fieldSelected)
        {
            return CheckInputSelectAnyOne(dataTable.DefaultView, fieldSelected);
        }

        public static bool CheckInputSelectAnyOne(DataView dataView, string fieldSelected)
        {
            bool flag = false;
            foreach (DataRowView view in dataView)
            {
                bool flag2 = true;
                if (view.Row[fieldSelected].ToString().ToUpper().Equals(flag2.ToString().ToUpper()))
                {
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                XtraMessageBox.Show(AppMessage.MSG0024, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            return flag;
        }

        public static void CheckTreeParentID(DataTable dataTable, string fieldID, string fieldParentID)
        {
            for (int i = dataTable.Rows.Count - 1; i >= 0; i--)
            {
                DataRow row = dataTable.Rows[i];
                if (dataTable.Select(fieldID + " = '" + row[fieldParentID].ToString() + "'").Length == 0)
                {
                    row[fieldParentID] = string.Empty;
                }
            }
        }

        public static void DataTableAddColumn(DataTable dataTable, string fieldName)
        {
            if ((dataTable != null) && !dataTable.Columns.Contains(fieldName))
            {
                DataColumn column = new DataColumn(fieldName, typeof(bool))
                {
                    DefaultValue = false,
                    AllowDBNull = false
                };
                dataTable.Columns.Add(column);
            }
        }

        public static void ExpandTreeNode(ECP.Utilities.Controls.ECPTreeList treeView)
        {
            for (TreeListNode node = treeView.FocusedNode; node != null; node = node.ParentNode)
            {
                node.Expanded=true;
            }
        }


        private static void FindTreeNode(TreeListNode treeNode, string id,ECP.Utilities.Controls.ECPTreeListColumn col)
        {
            for (int i = 0; i < treeNode.Nodes.Count; i++)
            {
                if (treeNode.Nodes[i].GetValue(col).ToString() == id)
                {
                    TargetNode = treeNode.Nodes[i];
                    return;
                }
                FindTreeNode(treeNode.Nodes[i], id,col);
            }
        }

        public static void FindTreeNode(ECP.Utilities.Controls.ECPTreeList treeView, string id,ECP.Utilities.Controls.ECPTreeListColumn col)
        {
            TargetNode = null;
            for (int i = 0; i < treeView.Nodes.Count; i++)
            {
                if (treeView.Nodes[i].GetValue(col)!=null &&treeView.Nodes[i].GetValue(col).ToString() == id)
                {
                    TargetNode = treeView.Nodes[i];
                    return;
                }
                FindTreeNode(treeView.Nodes[i], id,col);
            }
        }

        public static string FromatStringWidth(Font font, string target, int width)
        {
            Image image = new Bitmap(1, 1);
            Graphics graphics = Graphics.FromImage(image);
            string text = target;
            SizeF ef = graphics.MeasureString(target, font);
            for (int i = target.Length - 1; i >= 0; i--)
            {
                if (ef.Width <= width)
                {
                    break;
                }
                text = target.Substring(0, i);
                ef = graphics.MeasureString(text, font);
            }
            graphics.Dispose();
            return text;
        }

        public static DataRow GetDataGridViewEntity(ECP.Utilities.Controls.ECPGridView targetDataGridView)
        {
            DataRow row = null;
            row = targetDataGridView.GetFocusedDataRow();
            //CurrencyManager manager = null;
            //if (targetDataGridView.DataMember == string.Empty)
            //{
            //    if (targetDataGridView.DataSource != null)
            //    {
            //        manager = (CurrencyManager)targetDataGridView.BindingContext[targetDataGridView.DataSource, string.Empty];
            //    }
            //}
            //else if (!string.IsNullOrEmpty(targetDataGridView.DataMember))
            //{
            //    manager = (CurrencyManager)targetDataGridView.BindingContext[targetDataGridView.DataMember];
            //}
            //if ((manager != null) && (manager.Count > 0))
            //{
            //    row = ((DataRowView)manager.Current).Row;
            //}
            return row;
        }

        public static string GetDataGridViewEntityID(ECP.Utilities.Controls.ECPGridView targetDataGridView, string fieldID)
        {
            string str = string.Empty;
            DataRow dataGridViewEntity = GetDataGridViewEntity(targetDataGridView);
            if (dataGridViewEntity != null)
            {
                str = dataGridViewEntity[fieldID].ToString();
            }
            return str;
        }

        public static Form GetForm(string assemblyName, string formName)
        {
            Type type = Assembly.Load(assemblyName).GetType(formName);
            if (type == null)
            {
                Type[] typs = Assembly.Load(assemblyName).GetTypes();
                foreach (Type item in typs)
                {
                    if (item.Name == formName)
                    {
                        type = item;
                        return (Form)Activator.CreateInstance(type);
                    }
                }
                type = Assembly.Load(assemblyName).GetType(formName, true, false);
            }
            return (Form)Activator.CreateInstance(type);
        }

        public static string[] GetIDs(DataTable dataTable, string fieldID)
        {
            string[] strArray = new string[0];
            string str = string.Empty;
            foreach (DataRow row in dataTable.Rows)
            {
                if (row.RowState != DataRowState.Deleted)
                {
                    string str2 = row[fieldID].ToString();
                    if (str2.Length > 0)
                    {
                        str = str + str2 + ",";
                    }
                }
            }
            if (str.Length > 1)
            {
                strArray = str.Substring(0, str.Length - 1).Split(new char[] { ',' });
            }
            return strArray;
        }

        public static int GetLanguageResource(object targetObject)
        {
            int num = 0;
            string key = string.Empty;
            FieldInfo[] fields = targetObject.GetType().GetFields(BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
            string str2 = string.Empty;
            for (int i = 0; i < fields.Length; i++)
            {
                key = fields[i].Name;
                FieldInfo info = fields[i];
                str2 = ResourceManagerWrapper.Instance.Get(key);
                if (str2.Length > 0)
                {
                    info.SetValue(targetObject, str2);
                    num++;
                }
            }
            return num;
        }

        public static int GetRowIndex(DataTable dataTable, string fieldID, string id)
        {
            int num = 0;
            bool flag = false;
            foreach (DataRowView view in dataTable.DefaultView)
            {
                if (view[fieldID].ToString() == id)
                {
                    flag = true;
                    break;
                }
                num++;
            }
            if (!flag)
            {
                num = 0;
            }
            return num;
        }

        public static string[] GetSelecteIDs(DataTable dataTable, string fieldID, string fieldSelected)
        {
            return GetSelecteIDs(dataTable.DefaultView, fieldID, fieldSelected);
        }

        public static string[] GetSelecteIDs(DataView dataView, string fieldID, string fieldSelected)
        {
            string[] strArray = new string[0];
            string str = string.Empty;
            foreach (DataRowView view in dataView)
            {
                if (view.Row.RowState != DataRowState.Deleted)
                {
                    bool flag = true;
                    if (view.Row[fieldSelected].ToString().ToUpper().Equals(flag.ToString().ToUpper()))
                    {
                        string str2 = view.Row[fieldID].ToString();
                        if (str2.Length > 0)
                        {
                            str = str + str2 + ",";
                        }
                    }
                }
            }
            if (str.Length > 1)
            {
                strArray = str.Substring(0, str.Length - 1).Split(new char[] { ',' });
            }
            return strArray;
        }

        public static DataRow GetSelecteRow(DataTable dataTable, string fieldSelected)
        {
            return GetSelecteRow(dataTable.DefaultView, fieldSelected);
        }

        public static DataRow GetSelecteRow(DataView dataView, string fieldSelected)
        {
            DataRow row = null;
            foreach (DataRowView view in dataView)
            {
                if (view.Row.RowState != DataRowState.Deleted)
                {
                    bool flag = true;
                    if (view.Row[fieldSelected].ToString().ToUpper().Equals(flag.ToString().ToUpper()))
                    {
                        row = view.Row;
                    }
                }
            }
            return row;
        }

        public static bool IsModfiedAnyOne(DataTable dataTable)
        {
            return IsModfiedAnyOne(dataTable, false);
        }

        public static bool IsModfiedAnyOne(DataTable dataTable, bool containSelectedColumn)
        {
            bool flag = false;
            foreach (DataRow row in dataTable.Rows)
            {
                if (row.RowState == DataRowState.Modified)
                {
                    for (int i = 0; i < dataTable.Columns.Count; i++)
                    {
                        if ((containSelectedColumn || (dataTable.Columns[i].ColumnName != BaseBusinessLogic.SelectedColumn)) && (row[i].ToString() != row[i, DataRowVersion.Original].ToString()))
                        {
                            flag = true;
                            break;
                        }
                    }
                }
                if (row.RowState == DataRowState.Deleted)
                {
                    return true;
                }
                if (row.RowState == DataRowState.Added)
                {
                    return true;
                }
            }
            return flag;
        }

        public static void LoadTreeNodes(DataTable dataTable, string fieldID, string fieldParentID, string fieldFullName, ECP.Utilities.Controls.ECPTreeList treeView)
        {
            treeView.DataSource = dataTable;         
        }

        public static bool NodeAllowDelete(TreeListNode treeNode)
        {
            if (treeNode == null)
            {
                return false;
            }
            return (treeNode.Nodes.Count == 0);
        }

        public static int SetLanguageResource(Form targetForm)
        {
            int num = 0;
            string key = string.Empty;
            string str2 = string.Empty;
            key = targetForm.Name;
            str2 = ResourceManagerWrapper.Instance.Get(key);
            if (str2.Length > 0)
            {
                targetForm.Text = str2;
            }
            for (int i = 0; i < targetForm.Controls.Count; i++)
            {
                num += SetLanguageResource(targetForm, targetForm.Controls[i]);
            }
            return num;
        }

        private static int SetLanguageResource(Form targetForm, Control targetControl)
        {
            int num = 0;
            string key = string.Empty;
            string str2 = string.Empty;
            if (targetControl is GroupBox)
            {
                SetLanguageResource(targetForm, (GroupBox)targetControl);
            }
            if (targetControl is Panel)
            {
                SetLanguageResource(targetForm, (Panel)targetControl);
            }
            if (((targetControl is Label) || (targetControl is CheckBox)) || ((targetControl is Button) || (targetControl is GroupBox)))
            {
                key = targetForm.Name + "_" + targetControl.Name;
                str2 = ResourceManagerWrapper.Instance.Get(key);
                if (str2.Length > 0)
                {
                    targetControl.Text = str2;
                    num++;
                }
            }
            if (targetControl is DataGridView)
            {
                DataGridView view = (DataGridView)targetControl;
                for (int i = 0; i < view.ColumnCount; i++)
                {
                    key = targetForm.Name + "_" + view.Columns[i].Name;
                    str2 = ResourceManagerWrapper.Instance.Get(key);
                    if (str2.Length > 0)
                    {
                        view.Columns[i].HeaderText = str2;
                        num++;
                    }
                }
            }
            return num;
        }

        private static int SetLanguageResource(Form targetForm, GroupBox groupBox)
        {
            int num = 0;
            for (int i = 0; i < groupBox.Controls.Count; i++)
            {
                num += SetLanguageResource(targetForm, groupBox.Controls[i]);
            }
            return num;
        }

        private static int SetLanguageResource(Form TargetForm, Panel TargetPanel)
        {
            int num = 0;
            for (int i = 0; i < TargetPanel.Controls.Count; i++)
            {
                num += SetLanguageResource(TargetForm, TargetPanel.Controls[i]);
            }
            return num;
        }

        public static Form ShowForm(string assemblyName, string formName)
        {
            Form form = GetForm(assemblyName, formName);
            form.Show();
            return form;
        }

        public static bool TreeNodeCanMoveTo(TreeListNode currentTreeNode, TreeListNode targetTreeNode,ECP.Utilities.Controls.ECPTreeListColumn col)
        {
            if (currentTreeNode != null)
            {
                if (currentTreeNode.Equals(targetTreeNode))
                {
                    return false;
                }
                if (currentTreeNode.ParentNode == targetTreeNode)
                {
                    return false;
                }
                for (int i = 0; i < targetTreeNode.Nodes.Count; i++)
                {
                    if (currentTreeNode.GetValue(col).ToString().Equals(targetTreeNode.Nodes[i].GetValue(col).ToString()))
                    {
                        return false;
                    }
                }
                while (targetTreeNode.ParentNode != null)
                {
                    targetTreeNode = targetTreeNode.ParentNode;
                    if (currentTreeNode.Equals(targetTreeNode))
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        public static void WriteException(Exception exception)
        {
            WriteException(exception, "Log.txt");
        }

        public static void WriteException(BaseUserInfo userInfo, Exception Exception)
        {
            string fileName = "Log.txt";
            if (userInfo.Username.Length > 0)
            {
                fileName = "Log_" + userInfo.Username + ".txt";
            }
            WriteException(Exception, fileName);
        }

        public static void WriteException(Exception Exception, string fileName)
        {
            if (BaseSystemInfo.LogException)
            {
                string path = BaseSystemInfo.StartupPath + @"\" + fileName;
                if (!File.Exists(path))
                {
                    new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite).Close();
                }
                StreamWriter writer = new StreamWriter(path, true, Encoding.Default);
                writer.WriteLine(DateTime.Now.ToString(BaseSystemInfo.DateTimeFormat) + " " + Exception.Message);
                writer.Close();
            }
        }

        public delegate bool CheckMoveEventHandler(string selectedID);

        public delegate bool OnCommnetEventHandler(string categoryID, string id);

        public delegate void SelectedIndexChangedEventHandler(string selectedID);
    }
}